package com.wisdytech.linkdcs.equipLifeCycle.check.action;


import java.util.List;

import com.wisdytech.common.handler.ResourceMatchingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipLifeCycle.check.dto.CheckSearchDto;
import com.wisdytech.linkdcs.equipLifeCycle.check.model.CheckDetailRecord;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckRecordService;
import com.wisdytech.linkdcs.equipLifeCycle.check.service.ICheckService;
import com.wisdytech.linkdcs.equipLifeCycle.check.vo.CheckVo;
import com.wisdytech.linkdcs.system.model.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  点检管理前端控制器
 * </p>
 *
 * @author dailibo
 * @since 2018-06-05
 */
@Controller
@RequestMapping("/equipLifeCycle/check/manage")
public class CheckController {
	
	private Logger logger =LoggerFactory.getLogger(getClass());
	
	@Autowired
	private ICheckService iCheckService;
	
	@Autowired
	private ICheckRecordService iCheckRecordService;
    @Autowired
    @Qualifier("resourceMatchingService")
    private ResourceMatchingService resourceMatchingService;
	@RequestMapping("/index")
	public String itemIndex(Model model, HttpServletRequest httpRequest){
        this.resourceMatchingService.getResourceName(httpRequest,model);
		return "lifeCycle/equipLifeCycle/check/manageIndex";
	}
	
	@RequestMapping("/getChecks/get.mvc")
	@ResponseBody
	public StandardTableResult<CheckVo> listChecks(CheckSearchDto dto,Filter filter){
		
		PageInfo<CheckVo>  checks =
				iCheckService.listEquipChecks(filter, dto);
		
		StandardTableResult<CheckVo> tables=
				new StandardTableResult<CheckVo>(checks.getTotal(),checks.getList());
		
		return tables;
	}
	
	@RequestMapping("/saveCheck/save.mvc")
	@ResponseBody
	public StandardResult saveChecks(
			String records,String equipCode) {
		
		SysUser user =BaseUtil.getCurrentUser();
		List<CheckDetailRecord> listRecords =
				 JSON.parseArray(records, CheckDetailRecord.class);
		
		
		return iCheckRecordService.addCheckRecord(listRecords, equipCode, user.getUsername())?
				StandardResult.getSuccessResult():StandardResult.getFailResult("保存失败");
	}
	
	
	
	
	
}

